package com.lagou.demo.service.impl;

import com.lagou.demo.dao.CodeDao;
import com.lagou.demo.pojo.Code;
import com.lagou.demo.service.ICodeService;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @Classname CodeServiceImpl
 * @Description TODO
 * @Date 2020/12/14 19:51
 * @Created by yang
 */
@Service
public class CodeServiceImpl implements ICodeService  {

    @Autowired
    private CodeDao codeDao;

    @Transactional
    public void insert(Code code) {
        Code newCode = new Code();
        newCode.setEmail(code.getEmail());
        Optional<Code> one = codeDao.findOne(Example.of(newCode));
        if (one.isPresent()) {
           code.setId(one.get().getId());
        }
        codeDao.save(code);
    }

    public Code findOne(Code code) {
        Example<Code> example = Example.of(code);
        Optional<Code> one = codeDao.findOne(example);
        code = one.get();
        return code;
    }

}
